package appapi.controller.oider;

import appapi.Request.*;
import appapi.Vo.AddSelfCheckItemVo;
import appapi.Vo.EditSelfCheckItemVo;
import appapi.common.ResponseBean;
import appapi.entity.basic.SelfcheckItemEntity;
import appapi.service.SelfCheckItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 自检项目
 *
 * @author luocheng
 * @version 1.0
 * @date 2023/05/25 14:21
 */
@Api(tags = "自检项目")
@RestController
@RequestMapping("/api/selfCheckItem")
public class SelfCheckItemController {
    private static final Logger logger = LoggerFactory.getLogger(ModuletypeController.class);
    @Autowired
    private SelfCheckItemService selfCheckItemService;
    /**
     * 列表
     */
    @GetMapping("/getList")
    @ApiOperation(value = "获取列表")
    public ResponseBean<SelfcheckItemEntity> list(@Valid SelfItemReq req) {
        try {
            List<SelfcheckItemEntity> dataList = selfCheckItemService.list(req);
            return ResponseBean.success(dataList);
        } catch (Exception e) {
            logger.error("获取列表异常", e);
            return ResponseBean.fail("获取列表异常");
        }
    }

    /**
     * 信息
     */
    @GetMapping("/getDetail")
    @ApiOperation(value = "获取详情")
    public ResponseBean detail(Long selfItemId) {
        try {
            if (selfItemId == null) {
                return ResponseBean.fail("ID不能为空");
            }
            SelfcheckItemEntity entity = selfCheckItemService.detail(selfItemId);
            if (entity != null) {
                return ResponseBean.success(entity);
            } else {
                return ResponseBean.fail("获取详情失败");
            }
        } catch (Exception e) {
            logger.error("获取详情异常", e);
            return ResponseBean.fail("获取详情异常");
        }
    }

    /**
     * 新增
     */
    @ApiOperation(value = "新增信息")
    @PostMapping(value = "/addInfo")
    public ResponseBean addInfo(@Validated  @RequestBody AddSelfCheckItemVo vo) {
//        try {
            SelfcheckItemEntity entity = vo.vo2Entity();
            entity.setCreateTime(LocalDateTime.now());
            entity.setUpdateTime(LocalDateTime.now());
            int count = selfCheckItemService.count(entity);
            if (count >= 1) {
                return ResponseBean.fail("名称已存在，不能重复添加");
            }
            int iRet = selfCheckItemService.insert(entity);
            if (iRet > 0) {
                return ResponseBean.success("保存成功");
            } else {
                return ResponseBean.fail("保存失败");
            }
//        } catch (Exception e) {
//            logger.error("新增异常", e);
//            return ResponseBean.fail("新增异常");
//        }
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改信息")
    @PostMapping(value = "/editInfo")
    public ResponseBean update(@RequestBody EditSelfCheckItemVo vo) {
        try {
            SelfcheckItemEntity entity = vo.vo2Entity();
            ;
            entity.setUpdateTime(LocalDateTime.now());
            int count = selfCheckItemService.count(entity);
            if (count >= 1) {
                return ResponseBean.fail("名称已存在，不能重复修改");
            }
            int iRet = selfCheckItemService.update(entity);
            if (iRet > 0) {
                return ResponseBean.success("更新成功");
            } else {
                return ResponseBean.fail("更新失败");
            }
        } catch (Exception e) {
            logger.error("更新异常", e);
            return ResponseBean.fail("更新异常");
        }
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除信息")
    @PostMapping(value = "/deleteInfo")
    public ResponseBean delete(@RequestBody DelSelfItemReq req) {
        try {
            if (req.getSelfItemId() == null) {
                return ResponseBean.fail("ID不能为空");
            }
            int iRet = selfCheckItemService.delete(req.getSelfItemId());
            if (iRet == -1) {
                return ResponseBean.fail("存在引用不能删除");
            }
            if (iRet == -2) {
                return ResponseBean.fail("对象不存在");
            }
            else if (iRet > 0) {
                return ResponseBean.success("删除成功");
            } else {
                return ResponseBean.fail("删除失败");
            }
        } catch (Exception e) {
            logger.error("删除异常", e);
            return ResponseBean.fail("删除异常");
        }
    }
}
